﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Windows.Data;
using System.Windows.Navigation;

using LEEAAttendanceControl;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.General;
using LEEAAttendanceControl.Web.Services;

using System.ServiceModel.DomainServices.Client;

using System.Collections.ObjectModel;

namespace LEEAAttendanceControl.Views
{
    public partial class Employees : Page
    {
        LEEAContext dc = new LEEAContext();

        
        Dictionary<int, String> departments = new Dictionary<int, string>();

        public Employees()
        {
            InitializeComponent();
            ChangeEmployees();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ClearEvents();

            myApp.AddEvent += NewEmployeeHandler;
            myApp.EditEvent += EditEmployeeHandler;
            myApp.DeleteEvent += DeleteEmployeeHandler;
            myApp.ChangeEmployeesEvent += ChangeEmployeesHandler;
        }

        //Change Employees Handler
        void ChangeEmployees()
        {
            EmployeeDataSource.QueryParameters.Clear();

            Parameter pc = new Parameter();
            pc.ParameterName = "companyid";
            pc.Value = ((App)Application.Current).CompanyID.Value;
            EmployeeDataSource.QueryParameters.Add(pc);

            Parameter pd = new Parameter();
            pd.ParameterName = "departmentid";
            pd.Value = ((App)Application.Current).DepartmentID.Value;
            EmployeeDataSource.QueryParameters.Add(pd);

            Parameter pe = new Parameter();
            pe.ParameterName = "employeeid";
            pe.Value = ((App)Application.Current).EmployeeId;
            EmployeeDataSource.QueryParameters.Add(pe);

            EmployeeDataSource.Load();
        }

        public void ChangeEmployeesHandler(object sender, EventArgs e)
        {
            ChangeEmployees();
        }
       
        private void EmployeeDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }
        
        public void NewEmployeeHandler(object sender, EventArgs e)
        {
            EmployeeEdit empedit = new EmployeeEdit();

            empedit.currEmp = new Employee();

            empedit.LoadData(No.Zero);
            empedit.Closed += new EventHandler(EmployeeEdit_Closed);

            empedit.Show();
        }

        public void EditEmployeeHandler(object sender, EventArgs e)
        {
            Employee curremp = GRDEmployee.SelectedItem as Employee;

            EmployeeEdit empedit = new EmployeeEdit();

            empedit.LoadData(curremp != null ? curremp.EmployeeID : No.Zero);
            empedit.Closed += new EventHandler(EmployeeEdit_Closed);

            empedit.Show();
        }

        public void EmployeeEdit_Closed(object sender, EventArgs e)
        {
            EmployeeDataSource.Load();

            App myApp = (App)Application.Current;

            myApp.RefreshStructureTree(sender, new EventArgs());
        }

        public void DeleteEmployeeHandler(object sender, EventArgs e)
        {
            if (MessageBox.Show(ApplicationStrings.DeleteMessage, ApplicationStrings.Delete
                , MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                EmployeeDataSource.DataView.Remove(GRDEmployee.SelectedItem);
                EmployeeDataSource.SubmitChanges();

                App myApp = (App)Application.Current;

                myApp.InvokeChangeEmployeesEvent(sender, e);
            }
        }
        
    }
}
